package com.hp.easyBuy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hp.easyBuy.dao.AddressDao;
import com.hp.easyBuy.dao.GoodsDao;
import com.hp.easyBuy.entity.TAddress;
import com.hp.easyBuy.entity.TGoods;
import com.hp.easyBuy.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, TAddress> implements AddressService {
    @Autowired
    private AddressDao addressDao;

    @Override
    public List<TAddress> getAddressList(Long userId) {

        //封装查询对象
        QueryWrapper<TAddress> wrapper = new QueryWrapper<>();
        wrapper.eq("USERID",userId);
        wrapper.orderByAsc("ISDEFAULT");    //升序排列
        return addressDao.selectList(wrapper);
    }

    //将传入的地址设置为默认地址
    @Override
    public boolean updateStatus(Integer addressId) {
        QueryWrapper<TAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ADDRESSID",addressId);
        TAddress address = addressDao.selectOne(queryWrapper);
        address.setIsdefault(1);
        return addressDao.updateById(address)>0;
    }
    //根据用户id获取地址列表,将默认地址排在第一行
    @Override
    public List<TAddress> list(Integer userid) {
        QueryWrapper<TAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("USERID",userid);
        queryWrapper.orderByAsc("ISDEFAULT");
        List<TAddress> list = addressDao.selectList(queryWrapper);
        return list;
    }
    //查询当前默认地址,设置为不默认
    @Override
    public boolean getAddress() {
        QueryWrapper<TAddress> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ISDEFAULT",1);
        TAddress address = addressDao.selectOne(queryWrapper);
        address.setIsdefault(2);
        return  addressDao.updateById(address)>0;
    }

}
